package com.feituan.core.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.TypeCouponsMapper;
import com.feituan.core.persistence.service.ICouponService;
import com.feituan.core.pojo.entity.TypeCoupons;
import com.feituan.core.pojo.request.TypeCouponsPageReq;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.service.ITypeCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TypeCouponsServiceImpl extends ServiceImpl<TypeCouponsMapper, TypeCoupons> implements ITypeCouponsService {
    @Autowired
    private TypeCouponsMapper typesMapper;

    @Autowired
    private ICouponService couponService;

    /**
     * 新增关系
     * @param typeCoupons typeCoupons
     */
    @Override
    public boolean save(TypeCoupons typeCoupons) {
        return this.typesMapper.insert(typeCoupons) > 0;
    }

    /**
     * 根据类型查询数组
     * @param type type
     */
    @Override
    public TypeCoupons selectByType(String type) {
        return this.typesMapper.selectByType(type);
    }

    /**
     * 分页查询
     * @param typeCouponsPageReq typeCouponsPageReq
     */
    @Override
    public PageData<TypeCoupons> queryPage(TypeCouponsPageReq typeCouponsPageReq) {
        Page<TypeCoupons> page = this.lambdaQuery()
                .orderByDesc(TypeCoupons::getId)
                .page(new Page<>(typeCouponsPageReq.getPage(), typeCouponsPageReq.getLimit()));
        PageData<TypeCoupons> build = PageData.build(page.getCurrent(), page.getTotal(), page.getRecords());
//        for (TypeCoupons record : build.getRecords()) {
//            // 获取每个coupon的详细信息
//                for (Coupons coupon : record.getCoupons()) {
//                    Coupon detailedCoupon = couponService.selectById(coupon.getCouponId());
//                    Coupon info = new Coupon();
//                    if (detailedCoupon != null) {
//                        info.setType(detailedCoupon.getType());
//                        info.setName(detailedCoupon.getName());
//                        info.setCouponMoney(detailedCoupon.getCouponMoney());
//                        info.setThreshold(detailedCoupon.getThreshold());
//                        info.setUseTime(detailedCoupon.getUseTime());
//                        info.setUseDates(detailedCoupon.getUseDates());
//                        info.setRemark(detailedCoupon.getRemark());
//                        info.setCouponExplain(detailedCoupon.getCouponExplain());
//                        info.setApplyProducts(detailedCoupon.getApplyProducts());
//                    }
//                }
//        }
//        return build;
        return PageData.build(page.getCurrent(), page.getTotal(), page.getRecords());
    }
}
